package gameserver

import (
	"github.com/fish-tennis/gentity/util"
	. "github.com/fish-tennis/gnet"
	"github.com/fish-tennis/gserver/cfg"
	"github.com/fish-tennis/gserver/game"
	"github.com/fish-tennis/gserver/logger"
	"github.com/fish-tennis/gserver/pb"
	"log/slog"
	"strings"
)

// 客户端字符串形式的测试命令,仅用于测试环境!
func onTestCmd(player *game.Player, packet Packet) {
	logger.Info("onTestCmd %v", packet.Message())
	req := packet.Message().(*pb.TestCmd)
	cmdStrs := strings.Split(req.GetCmd(), " ")
	if len(cmdStrs) == 0 {
		player.SendErrorRes(packet.Command(), "empty cmd")
		return
	}
	cmdKey := strings.ToLower(cmdStrs[0])
	cmdArgs := cmdStrs[1:]
	switch cmdKey {
	case strings.ToLower("AddExp"):
		// 加经验值
		if len(cmdArgs) != 1 {
			player.SendErrorRes(packet.Command(), "AddExp cmdArgs error")
			return
		}
		value := int32(util.Atoi(cmdArgs[0]))
		if value < 1 {
			player.SendErrorRes(packet.Command(), "AddExp value error")
			return
		}
		player.GetBaseInfo().IncExp(value)

	case strings.ToLower("AddItem"):
		// 加物品
		if len(cmdArgs) < 1 {
			player.SendErrorRes(packet.Command(), "AddItem cmdArgs error")
			return
		}
		itemCfgId := int32(util.Atoi(cmdArgs[0]))
		itemCfg := cfg.GetItemCfgMgr().GetItemCfg(itemCfgId)
		if itemCfg == nil {
			player.SendErrorRes(packet.Command(), "AddItem itemCfgId error")
			return
		}
		itemNum := int32(1)
		if len(cmdArgs) >= 2 {
			itemNum = int32(util.Atoi(cmdArgs[1]))
		}
		if itemNum < 1 {
			player.SendErrorRes(packet.Command(), "AddItem itemNum error")
			return
		}
		player.GetBag().AddItem(itemCfgId, itemNum)

	case strings.ToLower("FinishQuest"), strings.ToLower("FinishQuests"):
		if len(cmdArgs) < 1 {
			player.SendErrorRes(packet.Command(), "FinishQuest cmdArgs error")
			return
		}
		// 完成所有任务
		if strings.ToLower(cmdArgs[0]) == "all" {
			for cfgId, _ := range player.GetQuest().Quests.Quests {
				player.GetQuest().OnFinishQuestReq(PacketCommand(pb.CmdQuest_Cmd_FinishQuestReq), &pb.FinishQuestReq{
					QuestCfgId: cfgId,
				})
			}
		} else {
			// 完成某一个任务
			cfgId := int32(util.Atoi(cmdArgs[0]))
			player.GetQuest().OnFinishQuestReq(PacketCommand(pb.CmdQuest_Cmd_FinishQuestReq), &pb.FinishQuestReq{
				QuestCfgId: cfgId,
			})
		}

	case strings.ToLower("Fight"):
		// 模拟一个战斗事件
		evt := &pb.EventFight{
			PlayerId: player.GetId(),
		}
		if len(cmdArgs) >= 1 && cmdArgs[0] == "true" {
			evt.IsPvp = true
		}
		if len(cmdArgs) >= 2 && cmdArgs[1] == "true" {
			evt.IsWin = true
		}
		player.FireConditionEvent(evt)

	case strings.ToLower("SignIn"):
		evt := &pb.EventPlayerPropertyInc{
			PlayerId:      player.GetId(),
			PropertyName:  "SignIn", // 签到事件
			PropertyValue: 1,
		}
		player.FireEvent(evt)

	case strings.ToLower("Pay"):
		if len(cmdArgs) < 1 {
			player.SendErrorRes(packet.Command(), "Pay cmdArgs error")
			return
		}
		payValue := int32(util.Atoi(cmdArgs[0]))
		player.GetBaseInfo().Data.TotalPay += payValue
		evt := &pb.EventPlayerPropertyInc{
			PlayerId:      player.GetId(),
			PropertyName:  "TotalPay",
			PropertyValue: payValue,
		}
		player.FireEvent(evt)

	case strings.ToLower("PlayerPropertyInc"):
		if len(cmdArgs) < 2 {
			player.SendErrorRes(packet.Command(), "PlayerPropertyInc cmdArgs error")
			return
		}
		evt := &pb.EventPlayerPropertyInc{
			PlayerId:      player.GetId(),
			PropertyName:  cmdArgs[0],
			PropertyValue: int32(util.Atoi(cmdArgs[1])),
		}
		player.FireEvent(evt)

	case strings.ToLower("AddActivity"):
		if len(cmdArgs) < 1 {
			player.SendErrorRes(packet.Command(), "AddActivity cmdArgs error")
			return
		}
		arg := cmdArgs[0]
		if arg == "all" {
			player.GetActivities().AddAllActivities(player.GetTimerEntries().Now())
		} else {
			activityId := int32(util.Atoi(arg))
			activityCfg := cfg.GetActivityCfgMgr().GetActivityCfg(activityId)
			player.GetActivities().AddNewActivity(activityCfg, player.GetTimerEntries().Now())
		}

	case strings.ToLower("ActivityReceiveReward"):
		if len(cmdArgs) < 2 {
			player.SendErrorRes(packet.Command(), "ActivityReceiveReward cmdArgs error")
			return
		}
		activityId := int32(util.Atoi(cmdArgs[0]))
		cfgId := int32(util.Atoi(cmdArgs[1]))
		activity := player.GetActivities().GetActivity(activityId)
		if activity == nil {
			return
		}
		if activityDefault, ok := activity.(*game.ActivityDefault); ok {
			activityDefault.ReceiveReward(cfgId)
		}

	case strings.ToLower("ActivityExchange"):
		if len(cmdArgs) < 2 {
			player.SendErrorRes(packet.Command(), "ActivityExchange cmdArgs error")
			return
		}
		activityId := int32(util.Atoi(cmdArgs[0]))
		cfgId := int32(util.Atoi(cmdArgs[1]))
		activity := player.GetActivities().GetActivity(activityId)
		if activity == nil {
			return
		}
		if activityDefault, ok := activity.(*game.ActivityDefault); ok {
			activityDefault.Exchange(cfgId)
		}

	case strings.ToLower("GuildRouteError"):
		// 模拟一个rpc错误,向一个不存在的公会发送rpc消息
		reply := new(pb.GuildJoinRes)
		err := player.GetGuild().RouteRpcToTargetGuild(123456789, PacketCommand(pb.CmdGuild_Cmd_GuildJoinReq),
			&pb.GuildJoinReq{Id: 123456789}, reply)
		if err != nil {
			slog.Info("GuildRouteError", "err", err.Error())
			return
		}
		slog.Debug("GuildRouteError reply", "reply", reply)

	default:
		player.SendErrorRes(packet.Command(), "unsupport test cmd")
	}
}
